﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MigraDoc.DocumentObjectModel;
using Payroll.BusinessLogic.CustomerManager;
using Payroll.BusinessLogic.Utils;
using Payroll.Common.Utils;
using PayrollHR.Web;

namespace PayrollHR.Administration.Modules
{
    public partial class CustomerAdd : BasePayrollAdministrationUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                pnlYesNo.Visible = false;
                this.BinCustomer();
            }
        }
        protected void BinCustomer()
        {
            ctrlCustomerInfo.FillDropDown();
            if (this.CustomerID == 0)
                return;
            var customer = this.CustomerService.GetCustomerById(this.CustomerID);
            if (CommonHelper.IsNull(customer))
                return;
            ctrlCustomerInfo.BindCustomer(customer);
            ctrlCaseProfileView.BindCaseProfilePersonal(customer);
        }
        protected Customer Save()
        {
            Customer customer = ctrlCustomerInfo.GetCustomer();
            customer = ctrlCaseProfileView.GetCustomer(customer);
            List<profile> ListProfile = ctrlCaseProfileView.GetProfile();
            List<ProfileProperty> ListProfileProperty = ctrlCaseProfileView.GetProfileProperty();
            List<ProfileFinan> ListProfileFinan = ctrlCaseProfileView.GetProfileFinan();
            List<FeesSchedule> ListFeesSchedule = ctrlCaseProfileView.GetFeesSchedule();
            List<Relationships> ListRelation = new List<Relationships>();
            ctrlRelationship.Relationships(ref ListRelation);
            this.CustomerService.Save(customer, ListProfile, ListProfileProperty, ListProfileFinan, ListFeesSchedule, ListRelation);

            return customer;
        }
        protected void SaveButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    Customer customer = ctrlCustomerInfo.GetCustomer();
                    if (customer.CustomerID == 0)
                    {
                        var cust = this.CustomerService.GetCustomerByName(customer.CustName);
                        if (CommonHelper.IsNull(cust))
                        {
                            SaveButton.Enabled = false;
                            SaveAndStayButton.Enabled = false;
                            this.Save();
                            Response.Redirect("Customers.aspx");
                        }
                        else
                        {
                            lbMessage.Text = String.Format("Khách hàng tên {0} đã có, bạn tiếp tục lưu nhấn button kế bên.", customer.CustName);
                            pnlYesNo.Visible = true;
                        }
                    }
                    else {
                        this.Continue_Click(sender, e);
                    }
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
                finally {
                    SaveButton.Enabled = true;
                    SaveAndStayButton.Enabled = true;
                }
            }
        }
        protected void SaveAndStayButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    Customer customer = ctrlCustomerInfo.GetCustomer();
                    if (customer.CustomerID == 0)
                    {
                        var cust = this.CustomerService.GetCustomerByName(customer.CustName);
                        if (CommonHelper.IsNull(cust))
                        {
                            SaveButton.Enabled = false;
                            SaveAndStayButton.Enabled = false;
                            customer = this.Save();
                            Response.Redirect(String.Format("CustomerAdd.aspx?CustomerId={0}", customer.CustomerID));
                        }
                        else
                        {
                            lbMessage.Text = String.Format("Khách hàng tên {0} đã có, bạn tiếp tục lưu nhấn button kế bên.", customer.CustName);
                            pnlYesNo.Visible = true;
                        }
                    }
                    else {
                        btnSaveAndStay_Click(sender, e);
                    }
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
                finally
                {
                    SaveButton.Enabled = true;
                    SaveAndStayButton.Enabled = true;
                }
            }
        }
        public int CustomerID
        {
            get
            {
                return CommonHelper.QueryStringInt("CustomerId");
            }
        }

        protected void btnPrintCustInfo_Click(object sender, EventArgs e)
        {
            if(this.CustomerID == 0)
            {
                ShowError("Customer does not exist.");
                return;
            }
            //
            string fileName = string.Format("CaseAllInfo_{0}_{1}.pdf", DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss"), CommonHelper.GenerateRandomDigitCode(4));
            string filePath = string.Format("{0}Administration\\FileExport\\{1}", HttpContext.Current.Request.PhysicalApplicationPath, fileName);
            string fileImage = string.Format("{0}images\\LogoOsiVietNam.jpg", HttpContext.Current.Request.PhysicalApplicationPath);
            Document doc = new Document();
            Customer customer;
            List<ProfileProperty> listProfileSupport;
            List<profile> listProfileStudy ;
            List<profile> listProfilePersonal ;
            List<profile> listProfileFinan ;
            List<profile> listProfileProperty ;
            List<ProfileFinan> listProfileFinanSource ;
            List<FeesSchedule> listProfileFeesShedule ;
            switch (RdButtonListOption.SelectedIndex)
            { 
                case 0:
                    customer = this.CustomerService.GetCustomerById(CustomerID);
                    listProfileSupport = CustomerService.GetAllProfilePropertyByCustomerId(CustomerID);
                    listProfileStudy = CustomerService.GetAllProfileByCustomerID(CustomerID, 5);
                    listProfilePersonal = CustomerService.GetAllProfileByCustomerID(CustomerID, 4);
                    listProfileFinan = CustomerService.GetAllProfileByCustomerID(CustomerID, 6);
                    listProfileProperty = CustomerService.GetAllProfileByCustomerID(CustomerID, 7);
                    listProfileFinanSource = CustomerService.GetAllByCustomerId(CustomerID);
                    listProfileFeesShedule = CustomerService.GetAllFeesScheduleByCustomerId(CustomerID);
                    doc = PDFHelper.PrintCustomerToPdf(customer, listProfileFeesShedule, fileImage, fileImage);
                    doc = PDFHelper.PrintCaseProfilePersonal(doc, listProfilePersonal, customer);
                    doc = PDFHelper.PrintCaseProfileStudy(doc, listProfileStudy, customer);
                    doc = PDFHelper.PrintCaseProfileFinan(doc, listProfileFinan, customer);
                    doc = PDFHelper.PrintCaseProfileProperty(doc, listProfileProperty, customer);
                    doc = PDFHelper.PrintCaseProfileFinanSource(doc, listProfileFinanSource);
                    doc = PDFHelper.PrintCaseProfileSupport(doc, listProfileSupport);
                    doc = PDFHelper.PrintCaseProfileFeesShedule(doc, listProfileFeesShedule);
                    break;
                case 1:
                    customer = this.CustomerService.GetCustomerById(CustomerID);
                    listProfileFeesShedule = CustomerService.GetAllFeesScheduleByCustomerId(CustomerID);
                    doc = PDFHelper.PrintCustomerToPdf(customer, listProfileFeesShedule, fileImage, fileImage);
                    break;
                case 2:
                    customer = this.CustomerService.GetCustomerById(CustomerID);
                    listProfileSupport = CustomerService.GetAllProfilePropertyByCustomerId(CustomerID);
                    listProfileStudy = CustomerService.GetAllProfileByCustomerID(CustomerID, 5);
                    listProfilePersonal = CustomerService.GetAllProfileByCustomerID(CustomerID, 4);
                    listProfileFinan = CustomerService.GetAllProfileByCustomerID(CustomerID, 6);
                    listProfileProperty = CustomerService.GetAllProfileByCustomerID(CustomerID, 7);
                    listProfileFinanSource = CustomerService.GetAllByCustomerId(CustomerID);
                    listProfileFeesShedule = CustomerService.GetAllFeesScheduleByCustomerId(CustomerID);
                    doc = PDFHelper.PrintCaseProfilePersonal(null, listProfilePersonal, customer);
                    doc = PDFHelper.PrintCaseProfileStudy(doc, listProfileStudy, customer);
                    doc = PDFHelper.PrintCaseProfileFinan(doc, listProfileFinan, customer);
                    doc = PDFHelper.PrintCaseProfileProperty(doc, listProfileProperty, customer);
                    doc = PDFHelper.PrintCaseProfileFinanSource(doc, listProfileFinanSource);
                    doc = PDFHelper.PrintCaseProfileSupport(doc, listProfileSupport);
                    doc = PDFHelper.PrintCaseProfileFeesShedule(doc, listProfileFeesShedule);
                    break;
                case 3:
                    customer = this.CustomerService.GetCustomerById(CustomerID);
                    listProfilePersonal = CustomerService.GetAllProfileByCustomerID(CustomerID, 4);
                    doc = PDFHelper.PrintCaseProfilePersonal(null, listProfilePersonal, customer);
                    break;
                case 4:
                    customer = this.CustomerService.GetCustomerById(CustomerID);
                    listProfileStudy = CustomerService.GetAllProfileByCustomerID(CustomerID, 5);
                    doc = PDFHelper.PrintCaseProfileStudy(null, listProfileStudy, customer);
                    break;
                case 5:
                    customer = this.CustomerService.GetCustomerById(CustomerID);
                    listProfileFinan = CustomerService.GetAllProfileByCustomerID(CustomerID, 6);
                    doc = PDFHelper.PrintCaseProfileFinan(null, listProfileFinan, customer);
                    break;
                case 6:
                    customer = this.CustomerService.GetCustomerById(CustomerID);
                    listProfileProperty = CustomerService.GetAllProfileByCustomerID(CustomerID, 7);
                    doc = PDFHelper.PrintCaseProfileProperty(null, listProfileProperty, customer);
                    break;
                case 7:
                    listProfileSupport = CustomerService.GetAllProfilePropertyByCustomerId(CustomerID);
                    doc = PDFHelper.PrintCaseProfileSupport(null, listProfileSupport);
                    break;
                case 8:
                    listProfileFinanSource = CustomerService.GetAllByCustomerId(CustomerID);
                    doc = PDFHelper.PrintCaseProfileFinanSource(null, listProfileFinanSource);
                    break;
                case 9:
                    listProfileFeesShedule = CustomerService.GetAllFeesScheduleByCustomerId(CustomerID);
                    doc = PDFHelper.PrintCaseProfileFeesShedule(null, listProfileFeesShedule);
                    break;
            }
            
            PDFHelper.PrintFile(doc, filePath);
            //string targetfile = string.Format("customers_{0}.pdf", DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss"));
            //CommonHelper.WriteResponsePdf(filePath, targetfile);
            //string fileName = string.Format("customers_{0}.xml", DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss"));

            hlexport.Visible = true;
            hlexport.Text = "Save As File Print";
            hlexport.NavigateUrl = "../FileExport/" + fileName;
        }

        protected void Cancel_Click(object sender, EventArgs e)
        {
            try {
                Response.Redirect(String.Format("CustomerAdd.aspx?CustomerId={0}", 0));
            }
            catch (Exception exc)
            {
                ProcessException(exc);
            }
        }

        protected void Continue_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    SaveButton.Enabled = false;
                    SaveAndStayButton.Enabled = false;
                    this.Save();
                    Response.Redirect("Customers.aspx");
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
                finally
                {
                    SaveButton.Enabled = true;
                    SaveAndStayButton.Enabled = true;
                }
            }
        }

        protected void btnSaveAndStay_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    SaveButton.Enabled = false;
                    SaveAndStayButton.Enabled = false;
                    Customer customer = Save();
                    Response.Redirect(String.Format("CustomerAdd.aspx?CustomerId={0}", customer.CustomerID));
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
                finally
                {
                    SaveButton.Enabled = true;
                    SaveAndStayButton.Enabled = true;
                }
            }
        }
    }
}